Four objects and one room, read in the order worth reading them - a real, working project to learn GBML from.
Top-down is one of the templates Gbabel ships (New project, or open templates/topdown directly) - 8-way movement with sliding collision, an enemy that chases, pickups that count, and a HUD counter, in four objects and one room. It is a real project, built by the exact same tool as any other, not a special-cased sample - which is what makes it worth reading rather than only skimming: every line in it is ordinary GBML you could have written yourself.
| Object | What it is |
|---|---|
obj_player | you |
obj_enemy | patrols a path until you get close, chases while you stay close, then walks back to the patrol and rejoins it |
obj_pickup | a gem you can walk onto to collect |
obj_wall | a solid obstacle |
rooms/room_main.json places one obj_player, one obj_enemy, four obj_pickups and eight obj_walls.
obj_player - read this one firstIts Step event is the whole point of the template:
var dx = btn_dx() * 2;
var dy = btn_dy() * 2;
if (dx != 0 && pos_free(x + dx, y, obj_wall)) { x += dx; }
if (dy != 0 && pos_free(x, y + dy, obj_wall)) { y += dy; }btn_dx/btn_dy read the d-pad as -1/0/1 on each axis, so all eight directions (and standing still) fall out of the same two lines. Checking each axis separately against pos_free - rather than testing the combined move in one go - is what turns this into a slide instead of a stop: running diagonally into a wall still lets the other axis through.
The rest of Step clamps the player to the room's own edge (a plain comparison against room_left, room_right, room_top and room_bottom - the room's four edges, counted from its top-left corner - since obj_wall only guards a few placed obstacles, not a ring around the room), then collects whatever pickup the player is currently standing on:
var got = collision_rect(box_left, box_top, box_right, box_bottom, obj_pickup);
if (got != nothing) {
pickups += 1;
instance_destroy(got);
}collision_rect returns a reference to the first overlapping instance (or nothing) rather than a plain true/false, which is what lets this code say which pickup to remove. Its Draw GUI event draws the resulting count fixed to the screen - draw_text_anchored(ANCHOR_TOP_LEFT, 4, 4, $"PICKUPS {pickups}") - a template string, read live every frame, no separate number-to-text conversion step. ((0, 0) is the screen's top-left pixel, so draw_text(4, 4, ...) in a Draw GUI event draws on exactly the same pixel; the anchor form is the one that also reaches the other corners without you writing the screen's size. Like every template, it counts from the top-left, as Where (0, 0) is teaches.)
obj_enemy - a patrol, a chase, and the handover between themrooms/room_main.json assigns this instance a patrol path - a closed, four-point rectangle (paths/patrol.json), LOOP, from The Paths tab - and starts it the moment the room places it. Its Step event then takes that patrol away and gives it back:
var target = instance_nearest(x, y, obj_player);
if (target != nothing) {
direction = dir_to(x, y, target.x, target.y);
speed = 1;
}That is the chase itself, and setting direction/speed is enough on its own - the engine's own motion phase turns them into movement right after Step returns. The real file wraps it in two instance variables, chasing and returning, which is what keeps the path and the chase from ever driving the same instance at once:
path_stop before it starts steering. A path only moves an instance while that instance is actively following one, so stopping the path first is what makes "the chase wins" a fact rather than a race.path_start on the spot. path_start snaps an instance onto its path's first point immediately - it does not walk there and does not resume mid-patrol - so calling it from the middle of the room would teleport the enemy. The file walks home to that first point under its own power (path_point_x/path_point_y read it straight off the path asset) and only hands back once standing on it, where the snap has nothing left to move.Unlike obj_player, none of this checks pos_free against obj_wall - the enemy beelines straight through obstacles while under its own power, deliberately, as an obvious first thing to change once you have read this far. (The patrol path itself never collides with anything either way.) Read objects/obj_enemy/step.gbml's own header for all three ideas in the template's own words; it is the longest comment in the project for a reason.
obj_wall and obj_pickup - four lines between themNeither has a Step event, because neither needs to do anything every frame. obj_wall sets solid = true in Create, which is the flag pos_free checks against. obj_pickup deliberately leaves solid false - the player has to be able to walk onto one to collect it, which a solid flag would prevent outright.
It has no painted tile layer or background - every wall you see on screen is a placed obj_wall sprite, not tilemap art, so the whole room's visual structure comes from instances rather than a painted floor. That is a real, disclosed scope choice for this template, not something left unfinished by accident.
Ctrl+B / ▶ Build & Play from inside the editor with this project open (see Building and reading its problems), or from a terminal:
python tools/gbabel_build.py templates/topdown --run